const puppeteer = require("puppeteer");

(async () => {
  const browser = await puppeteer.launch({ headless: false });

  const page = await browser.newPage();

  //   说明，以下代码我是开启了项目 demo.html
  await page.goto("http://127.0.0.1:5500/demo.html");

  //   page.on("request", (req) => {
  //     console.log("req.headers()", req.headers());
  //     console.log("req.method()", req.method());
  //     console.log("postData()", req.postData());
  //   });

  //   page.on("response", async (res) => {
  //     const info = await res.text();
  //     console.log("info", info);
  //   });

  //   paeg $ 方法 page.$('selector',handler);

  //   const btn = await page.$("button");
  //   await btn.click();

  // page $eval 方法 page.$eval('selector',callback,[...args])

  //   const params = {
  //     p1: "p1",
  //     p2: 30,
  //     p3: {
  //       data: [1, 2, 3, 4],
  //     },
  //   };

  //   const saveImg = (path) => {
  //     console.log("有人调用 saveImg 方法了", path);
  //   };

  //   const data = await page.$eval(
  //     "h1",
  //     (h1, saveImg) => {
  //       // 这里的环境是在 puppeteer 浏览器中，不是在 外部环境，因此 log 在打开的浏览器中看
  //       // 返回的结果作为方法的结果
  //       console.log("puppeteer 内核浏览器", h1);
  //       console.log("外部参数", saveImg);
  //     },
  //     saveImg
  //   );

  //   console.log("page.$eval 结果：", data);

  //   注入 script
  //   await page.addScriptTag({
  //     content:
  //       "const saveImg = (path) => console.log('调用了 saveImg方法，path=', path); ",
  //   });

  //   page.click("button");
  //   await page.goto("https://www.baidu.com");
  //   const cookies = await page.cookies("https://www.baidu.com");
  //   console.log(cookies);

  //   page.exposeFunction()
  //   const saveImg = (path) => {
  //     console.log("有人调用 saveImg 方法了", path);
  //   };
  //   //   添加方法
  //   await page.exposeFunction("saveImg", (path) => saveImg(path));

  //   page.evaluate(() => {
  //     console.log("puppeteer 内核浏览器");
  //     saveImg("/img/test");
  //   });

  //   page setCookies
  //   await page.setCookie({
  //     name: "BD_UPN",
  //     value: "12314753",
  //   });

  //   const cookies =
  //     "BIDUPSID=65FFB4F5A16D6F59EB06E87E1867EF37;PSTM=1687686902;BD_UPN=12314753;BAIDUID=CB20C3D67CCD8B93D39921E2946C993D:FG=1;BDSFRCVID=Os-OJeC62GGgUc6f6GjcuynBeVfAK7RTH6aoZbCZpn4mW2yHS0aREG0P2f8g0KuM85gVogKK0mOTH6KF_2uxOjjg8UtVJeC6EG0Ptf8g0f5;H_BDCLCKID_SF=tbPJoC0MJCL3eJjR2JD_-PAe5f8sWbtLB2Q-5KL-3h3JSJ8Cb6jnM4t02fTGKl5h0m0fbfbdJJjooRoJWR3_j-uiDlr7Q5OKLeTxoUt-5DnJhhvGXjAhbpKebPRiJPr9Qg-qahQ7tt5W8ncFbT7l5hKpbt-q0x-jLTnhVn0M5DK0HPonHj-BDTb03f;BDORZ=B490B5EBF6F3CD402E515D22BCDA1598;BDSFRCVID_BFESS=Os-OJeC62GGgUc6f6GjcuynBeVfAK7RTH6aoZbCZpn4mW2yHS0aREG0P2f8g0KuM85gVogKK0mOTH6KF_2uxOjjg8UtVJeC6EG0Ptf8g0f5;H_BDCLCKID_SF_BFESS=tbPJoC0MJCL3eJjR2JD_-PAe5f8sWbtLB2Q-5KL-3h3JSJ8Cb6jnM4t02fTGKl5h0m0fbfbdJJjooRoJWR3_j-uiDlr7Q5OKLeTxoUt-5DnJhhvGXjAhbpKebPRiJPr9Qg-qahQ7tt5W8ncFbT7l5hKpbt-q0x-jLTnhVn0M5DK0HPonHj-BDTb03f;BAIDUID_BFESS=CB20C3D67CCD8B93D39921E2946C993D:FG=1;BD_HOME=1;delPer=0;BD_CK_SAM=1;PSINO=2;BA_HECTOR=akah8g04840l01a48h20a10e1ib7gc61p;ZFY=cnVewQpWdNYg7HiX1ZLmjbsDgxMWsohlewDhAx00kGs:C;H_PS_645EC=4db01GhMm6Ffl3vApzMMEVdfk3QcILPxWpAvsRjsswirFY9S%2FXV7g0%2FmdpA;H_PS_PSSID=36560_38643_38831_39027_39022_38943_38877_38957_38954_39009_39037_38818_38827_38990_39086_38639_26350_39042_39095_22160_39100_38683;B64_BOT=1;channel=baidusearch;baikeVisitId=fc4e3aba-f2a6-47c2-a2fb-4aae994ac343";
  //   const arr = cookies.split(";");
  //   arr.forEach(async (i) => {
  //     const [name, value] = i.split("=");
  //     let obj = { name, value };
  //     await page.setCookie(obj);
  //   });

  // page type 元素输入
  // await page.type("input", "page.type");

  // page 的请求对象及响应对象 Request、Response
  await page.setRequestInterception(true);
  page.on("request", (req) => {
    // req.abort();
    const url = req;

  });
  //   browser.close();
})();
